using System;
using System.Collections.Generic;
using System.Text;
using NHibernate;


namespace Rensselaer_Event_Finder.Hibernate.DAO
{
   
    /// The service that facilitates interaction between the database
    ///and the application for the Location class
     
    public class LocationDAO : GenericNHibernateDAO<Location, int>
    {
        /// <summary>
        ///  Get all locations for a specific category
        /// </summary>
        /// <param name="categoryId"></param>
        /// <returns></returns>
 
        public IList<Location> getByCategoryId(int categoryId)
        {
            IQuery q = Session.GetNamedQuery("Location.byCategoryId");
            q.SetInt32("categoryId", categoryId);
            return q.List<Location>();
        }

        /// <summary>
        /// Get all locations that have the text string in either the description, category, name, or comment 
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
 
        public IList<Location> getByTextSearch(String text)
        {
            IQuery q = Session.GetNamedQuery("Location.byTextSearch");
            q.SetString("text", "%" + text.ToLower() + "%");
            return q.List<Location>();
        }

        /// <summary>
        ///  Get the newest X locations 
        /// </summary>
        /// <param name="count"></param>
        /// <returns></returns>

        public IList<Location> getNewestLocations(int count)
        {
            IQuery q = Session.GetNamedQuery("Location.newest").SetMaxResults(count);
            return q.List<Location>();
        }
        /// <summary>
        /// obtain the locations with the highest ratings
        /// </summary>
        /// <param name="count"></param>
        /// <returns></returns>
        public IList<Int32> getHighestRatedLocations(int count)
        {
            IQuery q = Session.CreateSQLQuery("select l.location_id from locations l left join location_ratings r on l.location_id = r.location_id group by l.location_id order by avg(r.rating) desc").SetMaxResults(count);
            return q.List<Int32>();
        }
    }
}
